<template>
	<view>
		<u-navbar :title="query.title"></u-navbar>
		<u-form ref="uForm" :modal="fromdata">
			<u-form-item v-for="(item, index) in inputs" :key="index" :label="item.label">
				<view class="u-flex"
					style="display: flex; align-items:center;justify-content: space-between;width: 100%;">
					<u-input v-model="fromdata[item.value]" :disabled="item.disabled" :placeholder="item.placeholder"
						:type="item.type" :border="false" />
					<wpasssmsbtn v-if="item.value == 'yzm'" ref="code" @click="getcode"></wpasssmsbtn>
					<u-image v-if="item.value == 'res'" width="175rpx" height="70rpx" :src="Kaptcha.img"
						@click="getWpassSmsAreaKaptcha"></u-image>
				</view>
			</u-form-item>
			<view class="" style="text-align: center; padding: 40rpx 20rpx">
				<u-button type="primary" size="medium" @click="updatafn">
					{{ $z.$t("提交内容") }}
				</u-button>
			</view>
		</u-form>
	</view>
</template>

<script>
	import config from "@/config/index.js";
	export default {
		data() {
			return {
				fromdata: {
					printid: "",
					yzm: "",
					res: "",
				},
				Kaptcha: {},
				WpassSmsfalg: true,
				query: {},
			};
		},
		onLoad(query) {
			this.query = query;
			this.fromdata = {
				...query,
				yzm: "",
				res: "",
			};
			this.getcard();
			console.log(this.fromdata, "this.fromdatathis.fromdata");
			// this.getcode();;
		},
		mixins: [config],
		computed: {
			inputs() {
				let r = [{
						label: "卡号",
						placeholder: "请输入卡号",
						value: "printid",
					},

				];
				if (this.WpassSmsfalg) {
					r = [{
							label: "卡号",
							value: "printid",
							placeholder: "请输入卡号",
							disabled: true
						},
						{
							label: "图形验证码",
							value: "res",
							placeholder: "请输入图形验证码",
							icon: "iconyanzhengma",
						},
						{
							label: "手机号码",
							value: "utel",
							placeholder: "请输入手机号码",
							type: "button",
						},
						{
							label: "短信验证码",
							placeholder: "请输入短信验证码",
							value: "yzm",
						},
					];
				}
				return r;
			},
			codestr() {
				return this.$refs.code ? this.$refs.code.str : "";
			},
		},
		mounted() {
			this.getWpassSmsAreaKaptcha();
		},
		methods: {
			async getWpassSmsAreaKaptcha() {
				let Kaptcha = await this.$z.request("/WpassSmsAreaKaptcha");
				Kaptcha.img = "data:image/png;base64," + Kaptcha.img;
				this.Kaptcha = Kaptcha;
			},
			async getcard() {
				if (this.fromdata.printid == "") {
					return;
				}
				let Card = await this.$z.request("/Card", {
					printid: this.fromdata.printid,
				});
				if (Card.length == 0) {
					this.$u.toast("卡号填写错误");
					return;
				}
				Card = Card[0];
				if (!Card.utel || Card.utel == "") {
					this.$u.toast("卡信息填写错误");
					return;
				}
				this.fromdata.utel =
					Card.utel.substr(0, 3) + "****" + Card.utel.substr(7);
				this.fromdata.codeprintid = this.fromdata.printid;
				this.fromdata.Cardutel = Card.utel
				this.WpassSmsfalg = true
			},
			async getcode() {
				this.WpassSmsfalg = true;
				if (this.fromdata.printid == "") {
					return;
				}

				if (this.fromdata.res == "") {
					this.$u.toast("请填写图形验证码");
					return;
				}
				this.$refs.code[0]
					.getCode(this.fromdata.Cardutel, "/WpassSmsAreaNoLogin", {
						area: "0",
						mda: this.Kaptcha.mda,
						res: this.fromdata.res,
					})
					.then(() => {
						// this.fromdata.utel =
						// 	Card.utel.substr(0, 3) + "****" + Card.utel.substr(7);
						// this.fromdata.codeprintid = this.fromdata.printid;
						// this.WpassSmsfalg = true;
					})
					.catch(() => {
						this.yzmerr();
					});
			},
			yzmerr() {
				this.getWpassSmsAreaKaptcha();
				this.fromdata.res = "";
				this.fromdata.yzm = "";
			},
			async updatafn() {
				if (this.fromdata.codeprintid == "") {
					return;
				}
				try {
					await this.$refs.code[0].codeyz(this.fromdata.yzm, this.fromdata.Cardutel);
					let par = await uni.$z.request("/" + this.query.url, {
						printid: this.fromdata.codeprintid,
					});
					if (par[0].par == "succeed") {
						await this.$store.dispatch("user/getuser", true);
						uni.navigateBack({
							delta: 2
						})
					} else {
						let str = config.pars[par[0].par] || ""
						this.$u.toast(str)
					}
				} catch (error) {
					this.yzmerr();
				}
			},
		},
	};
</script>

<style></style>